﻿#include "login_dialog.h"
#include "ui_login_dialog.h"
#include "home_dialog.h"
#include "cash_produce_dialog.h"
#include <QMessageBox>
#include <QSqlQuery>
//登陆界面
login_dialog::login_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login_dialog)
{
    ui->setupUi(this);
}

login_dialog::~login_dialog()
{
    delete ui;
}

void login_dialog::on_loginBtn_clicked()
{
    if(ui->userName->text().isEmpty()){
        //判断用户名是否为空
        QMessageBox::about(NULL, "警告", "用户名为空！");
        ui->userName->setFocus();
        return;
    }
    //login_dialog::USER = ui->userName->text();
    QString name = ui->userName->text().trimmed(); //取出用户名
    QString password = ui->passWord->text();
    int type;

    //使用ComboBox中的数据来验证信息
    if(ui->typeCBox->currentText() == "店长"){type = 1;}
    if(ui->typeCBox->currentText() == "店员"){type = 2;}

    QSqlQuery query(tr("select username from User"));
    bool hasUser = false;
    while(query.next()){
        if(query.value(0).toString() == name){
            hasUser = true;
            break;
        }
    }
    if(hasUser){
        //有这个用户
        query.exec(QString("select password,userstyle from User where username='" +name+ "'"));

        query.next();
        if((query.value(0).toString() == password) && (query.value(1).toInt() == 1 && type == 1)){
            //跳转到管理员界面
                this->close();
                home_dialog *home = new home_dialog();
                home->exec();
          //  accept();
        }else if(query.value(0).toString() == password && (query.value(1).toInt() == 2 && type==2)){
            //直接跳转收银员界面
                this->close();
                cash_produce_dialog *cash = new cash_produce_dialog();
                //发送登陆用户名到收银界面显示，USER为cash_produce_dialog的常量
                cash->USER = this->ui->userName->text();
                cash->init();
                cash->exec();
        }
        else
        {
            QMessageBox::about(NULL, "错误", "密码不正确！");
            ui->passWord->selectAll();
            ui->passWord->setFocus();
        }
    }else {
        QMessageBox::about(NULL, "错误", "对不起没有该用户");
        ui->userName->selectAll();
        ui->userName->setFocus();
    }
}


